//
//  HMNavigationController.swift
//  WeiboZH
//
//  Created by acer on 16/10/5.
//  Copyright © 2016年 acer. All rights reserved.
//

import UIKit

class HMNavigationController: UINavigationController, UIGestureRecognizerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        //  设置边缘手势代理
        self.interactivePopGestureRecognizer?.delegate = self
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /**
     重写此方法，在里面统一返回按钮逻辑
     
     - parameter viewController: 将要push进来的控制器
     - parameter animated:       是否需要动画
     */
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        // 如果当前里面有控制器，才执行下面的逻辑
        if childViewControllers.count > 0 {
//            方法1
//            //  表示当前是第二个视图控制器
//            if viewControllers.count == 1 {
//                
//                //  获取上一级控制的title
//                let title = viewControllers.first!.title!
//                
//                viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: title, imageName: "navigationbar_back_withtext", target: self, action: #selector(backAction))
//            } else {
//                
//                viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "返回", imageName: "navigationbar_back_withtext", target: self, action: #selector(backAction))
//            }
//            viewController.title = "当前显示的第\(viewControllers.count + 1)级控制器"
            
            //方法2
//            var title = "返回"
//            
//            if sinaBack {
//                if childViewControllers.count == 1 {
//                    //正要往里面添加第二个控制器
//                    title = childViewControllers.first?.title ?? "返回"
//                }
//            }
//            viewController.title = "当前显示的第\(viewControllers.count + 1)级控制器"
//            // push第二个或者以后控制器隐藏底部tabBar
//            viewController.hidesBottomBarWhenPushed = true
//            // 设置左边item
//            viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: title, imageName: "navigationbar_back_withtext", target: self, action: #selector(backAction))
            //  表示当前是第二个视图控制器
            if viewControllers.count == 1 {
                
                //  获取上一级控制的title
                let title = viewControllers.first!.title!
                
                viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: title, imageName: "navigationbar_back_withtext", target: self, action: #selector(backAction))
                
                
                
                
            } else {
                
                viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "返回", imageName: "navigationbar_back_withtext", target: self, action: #selector(backAction))
                
                
            }
            
            viewController.title = "当前显示的第\(viewControllers.count + 1)级控制器"
            //  隐藏底部tabbar
            viewController.hidesBottomBarWhenPushed = true

        }
        
        super.pushViewController(viewController, animated: animated)
    }
    
    func backAction()  {
        
        popViewController(animated: true)
        
        
    }
    
    //  是否处理这次开始点击的手势
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        //  如果是根视图控制器的话,不处理边缘手势
        if viewControllers.count == 1 {
            return false
        }
        return true
    }

}
